package com.puny.mvc.controllers.keyscanner
{
	import com.puny.mvc.MvcEvent;
	import com.puny.testframework.TestCaseWithStage;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	public class KeyControllerTest extends TestCaseWithStage
	{
		private var _s:String = "s";
		private var _t:String = "t";
		private var _d:String = "d";
		private var _u:String = "u";
		private var _key:String;
		private var _xml:XML = new XML('<key><left value="'+_s+'"/><enter/><right value="'+_t+'" once="true"/><down value="'+_d+'" once="false"/></key>');
		public function KeyControllerTest(methodName:String=null)
		{
				super(methodName);
		}
		
		public function testKeyScanner1():void {
			var ctrl:KeyController = new KeyController(_xml);
			ctrl.addEventListener(_xml.left[0].localName()+KeyController.DOWN, pressed);
			ctrl.addEventListener(MvcEvent.READY, readyHandler);
			_key = _s;
			mainStage.addChild(ctrl);
		}

		public function testKeyScanner2():void {
			var ctrl:KeyController = new KeyController(_xml);
			ctrl.addEventListener(_xml.right[0].localName()+KeyController.DOWN, pressed);
			ctrl.addEventListener(MvcEvent.READY, addAsync(readyHandler,3000));
			_key = _t;
			mainStage.addChild(ctrl);
		}

		public function testKeyScanner3():void {
			var ctrl:KeyController = new KeyController(_xml);
			ctrl.addEventListener(_xml.down[0].localName()+KeyController.DOWN, pressed);
			ctrl.addEventListener(MvcEvent.READY, readyHandler);
			_key = _d;
			mainStage.addChild(ctrl);
		}

		public function testKeyScanner4():void {
			var ctrl:KeyController = new KeyController(_xml);
			ctrl.addEventListener(_xml.right[0].localName()+KeyController.DOWN, pressed);
			ctrl.addEventListener(MvcEvent.READY, addAsync(readyHandler,3000));
			_key = _u;
			mainStage.addChild(ctrl);
		}
		
		public function testKeyScanner5():void {
			var ctrl:KeyController = new KeyController(_xml,mainStage);
			ctrl.addEventListener(_xml.enter[0].localName()+KeyController.DOWN, pressed);
			ctrl.addEventListener(MvcEvent.READY, addAsync(readyHandler,3000));
			_key = String.fromCharCode(Keyboard.ENTER);
			mainStage.addChild(ctrl);
		}
		
		private function readyHandler(event:Event):void {
			var timer:Timer = new Timer(50,10);
			timer.addEventListener(TimerEvent.TIMER, timerHandler);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(completeHandler,3000));
			timer.start();
		}
		
		private function completeHandler(event:Event):void {
			switch(_key) {
				case _t:
					assertTrue(_count==1);
					break;
				case _s:
				case _d:
				case String.fromCharCode(Keyboard.ENTER):
					assertTrue(_count>1);
					break;
				default:
					assertTrue(_count==0);
					break;
			}
			trace("keyUp",_key,_key.charCodeAt(0));
			mainStage.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_UP,false,false,_key.charCodeAt(),_key.charCodeAt()));
		}
		
		private function timerHandler(event:Event):void {
			trace("keyDown",_key,_key.charCodeAt(0));
			mainStage.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN,false,false,_key.charCodeAt(),_key.charCodeAt()));
		}
		
		private var _count:int=0;
		private function pressed(event:Event):void {
			_count++;
			trace(_key," was pressed",_count);
		}
	}
}